﻿layui.config({
	base: "/Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage', 'table'], function () {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		table = layui.table;
	//校区管理列表页面
	var ManageFunction = {
		//加载列表
		showList: function () {
			table.render({
				elem: '#tb_MajorList'
				//, height: 315
			  , url: '/MajorManage/GetMajorList' //数据接口
			  , id: 'schoolListTable'
			  , page: true //开启分页
			  , method: 'post'
			  , cols: [[ //表头    
				  { field: 'Id', title: '专业ID', width: 200 }
				, { field: 'MajorName', title: '专业名称', width: 200, }
				, { field: 'AddTime', title: '添加时间', templet: '<div>{{ FormatJsonTime(d.AddTime) }}</div>', width: 200, }
				, { toolbar: '#barDemo', title: '操作', width: 450, }
			  ]]
			});
			//监听工具条
			table.on('tool(majortool)', function (obj) {
				var data = obj.data;
				if (obj.event === 'del') {
					layer.confirm('真的删除行么', function (index) {
						//obj.del();
						ManageFunction.deleteList(data);
						layer.close(index);
					});
				}
				else if (obj.event === 'add') {
					ManageFunction.showAddCourseForm(data);
				}
				else if (obj.event === 'edit') {
					ManageFunction.showAddForm(data);
				}
				else if (obj.event === 'info') {
					ManageFunction.showInfoForm(data);
				}
			});
		},
		//搜索列表信息
		searchList: function () {
			var schoolNameVal = $('#majorName');
			//执行重载
			table.reload('schoolListTable', {
				page: {
					curr: 1 //重新从第 1 页开始
				}
			  , where: {
				  majorName: schoolNameVal.val(),
			  }
			});
		},
		//添加修改页面展示
		showAddForm: function (data) {
			var titleName = "添加专业";
			var url = "/MajorManage/AddMajor";
			if (data != null) {
				url = "/MajorManage/EditMajor/" + data.Id;
				titleName = "修改专业";
			}
			var index = layui.layer.open({
				title: titleName,
				type: 2,
				anim: 1,
				resize: true,
				maxmin: true,
				area: ['500px', '255px'],
				content: url,
				success: function (layero, index) {
				}
			})
		},
		//校区详情
		showInfoForm: function (data) {
			var index = layui.layer.open({
				title: "校区详情",
				type: 2,
				anim: 1,
				resize: true,
				maxmin: true,
				area: ['800px', '500px'],
				content: "/MajorManage/MajorInfo/" + data.Id,
				success: function (layero, index) {
				}
			})
			//layui.layer.full(index);
		},
		//添加专业对应课程
		showAddCourseForm: function (data) {
			var index = layui.layer.open({
				title: "编辑专业对应课程",
				type: 2,
				anim: 1,
				resize: true,
				maxmin: true,
				area: ['800px', '500px'],
				content: "/MajorManage/AddMajorCourse/" + data.Id,
				success: function (layero, index) {
				}
			})
			//layui.layer.full(index);
		},
		//loadMajorCourse: function (SemesterType) {
		//	var majorId = $("#majorId").val();
		//	if (majorId == null || !parseInt(majorId)) {
		//		majorId = 0;
		//	}
		//	$.post("/MajorManage/GetMajorCourseList/", { majorId: majorId, SemesterType: SemesterType }, function (result) {
		//		$.each(result, function (k, v) {
		//			var content = "";
		//			if (v.MajorId != 0) {
		//				content = '<input type="checkbox" name="Course" class="homePage" title="' + v.CourseName + '" checked>';
		//			} else {
		//				content = '<input type="checkbox" name="Course" class="homePage" title="' + v.CourseName + '">';
		//			}
		//			$("#courseList").append(content);
		//		});
		//		form.render('checkbox');
		//	}, "json");
		//},
		//删除列表信息
		deleteList: function (data) {
			$.post("/MajorManage/DeleteMajor", { id: data.Id }, function (result) {
				if (result.ResultStatus == 1) {
					layer.msg(result.Message, function () {
						window.location.reload();
					});
				}
				else {
					layer.msg(result.Message, { icon: 6 });
				}
			});
		},
		//操作返回结果
		resultEditForm: function (FormName, URL) {
			var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
			$("#" + FormName).ajaxSubmit({
				url: URL,　　　　　　//form提交数据的地址
				type: "post",　　　  //form提交的方式(method:post/get)
				//target:target,　　//服务器返回的响应数据显示在元素(Id)号确定
				// beforeSubmit:function(){},　　//提交前执行的回调函数
				success: function (dataRes) {
					if (dataRes.ResultStatus == 1) {
						top.layer.close(index);
						top.layer.msg(dataRes.Message);
						layer.closeAll("iframe");
						parent.location.reload();
					}
					else {
						top.layer.close(index);
						top.layer.msg(dataRes.Message);
					}
				},//提交成功后执行的回调函数
				dataType: "json",　　　　　　　//服务器返回数据类型
				clearForm: true,　　　　　　 //提交成功后是否清空表单中的字段值
				restForm: true,　　　　　　  //提交成功后是否重置表单中的字段值，即恢复到页面加载时的状态
				timeout: 6000 　　　　　 　 //设置请求时间，超过该时间后，自动退出请求，单位(毫秒)。
			});
		}
	};
	//加载页面数据
	ManageFunction.showList();

	//查询
	$(".search_btn").click(function () {
		ManageFunction.searchList();
	})

	//添加展示页面
	$(".usersAdd_btn").click(function () {
		ManageFunction.showAddForm();
	})

	//添加列表信息
	form.on("submit(addMsg)", function (data) {
		var url = "/MajorManage/AddMajor";
		if (parseInt($("#Id").val()) > 0) {
			url = "/MajorManage/EditMajor";
		}
		ManageFunction.resultEditForm("addMajorForm", url);
		return false;
	})
	//监听学期选项
	form.on("radio(SemesterType)", function (data) {
		$("#courseList").empty();
		loadMajorCourse(data.value);
	});

	//返回列表，刷新父页面
	$("body").on("click", ".backlist", function () {
		parent.location.reload();
	})
})
var jQuery;
layui.use(['form'], function () {
	form = layui.form;
});
function  loadMajorCourse(SemesterType) {
	var majorId = $("#majorId").val();
	if (majorId == null || !parseInt(majorId)) {
		majorId = 0;
	}
	$.post("/MajorManage/GetMajorCourseList/", { majorId: majorId, SemesterType: SemesterType }, function (result) {
		$.each(result, function (k, v) {
			var content = "";
			if (v.MajorId != 0) {
				content = '<input type="checkbox" name="Course" class="homePage" title="' + v.CourseName + '" checked>';
			} else {
				content = '<input type="checkbox" name="Course" class="homePage" title="' + v.CourseName + '">';
			}
			$("#courseList").append(content);
		});
		form.render('checkbox');
	}, "json");
}




